package com.medical.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.dao.SysAreaMapper;
import com.medical.entity.SysArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class AreaService {
    @Autowired
   private SysAreaMapper areaMapper;
    /**
     * 添加区域
     * @param area
     */
    public void add(SysArea area){

        area.setCreateTime(new Timestamp(System.currentTimeMillis()));
        area.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        areaMapper.insert(area);
}
    /**
     * 删除区域
     * @param id
     */
    public void delete(Integer id){
        if( id != null){
            areaMapper.deleteByPrimaryKey(id);
        }

}
    /**
     * 修改区域
     * @param area
     */
    public void edit(SysArea area){
        area.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        areaMapper.updateByPrimaryKeySelective(area);
    }
    /**
     * 根据ID主键查找区域信息
     * @param id
     * @return
     */
    public SysArea getAreaById(Integer id){return areaMapper.selectByPrimaryKey(id);}
    /**
     * 分页查找
     * @param areaName
     * @param currentPage
     * @param limit
     * @return
     */
    public PageInfo<SysArea> getAllArea(String areaName, Integer currentPage, Integer limit) {
        //利用PageHelper分页查询 注意：这个一定要放在查询语句的第一行，否者无法进行分页，因为他对金随后的第一个sql语句
        PageHelper.startPage(currentPage,limit);
        List<SysArea> list = areaMapper.getAllArea(areaName);
        PageInfo<SysArea> pageInfo = new PageInfo<SysArea>(list);
        return pageInfo;
    }
    public List<SysArea> getAreaTree(String areaName) {
        return areaMapper.getAllArea(areaName);
    }

}
